<template>
	<view class="publish-payment">
		<view class="item-wrap">
			<view class="item">
				<view class="title">单条广告价格</view>
				<view class="title">{{amount}}元</view>
			</view>
			<view class="item">
				<view class="title">购买条数</view>
				<input type="number" class="input" value="" :disabled="disableInput" v-model="number" placeholder="请输入购买条数" />
			</view>
			<view class="item">
				<button class="button" hover-class="button-active" @click="showDetail()">计算费用</button>
			</view>
		</view>

		<view v-if="isShowDetail" class="item-wrap">
			<uni-collapse>
				<uni-collapse-item :title="totalFee" show-arrow>
					<view class="item" v-if="showProcutFee&&(userType==99 || userType==10)">
						<view class="title">产品服务费</view>
						<view class="title">{{serviceAmount?serviceAmount/100:0}}元</view>
					</view>
					<!-- <view class="item" v-if="showProcutFee&&userType==99">
						<view class="title">产品管理费</view>
						<view class="title">{{adsellAmount/100}}元</view>
					</view> -->
					<view class="item">
						<view class="title">平台管理费</view>
						<view class="title">{{platAmount/100}}元</view>
					</view>
					<view class="item">
						<view class="title">广告需支付金额</view>
						<view class="title">{{adAmount/100}}元</view>
					</view>
					<view class="item" v-if="channelId!=='2'">
						<view class="title">商家宣传收益</view>
						<view class="title">{{mchAmount/100}}元</view>
					</view>
				</uni-collapse-item>
			</uni-collapse>
			<uni-collapse>
				<uni-collapse-item title="费用说明">
					<view class="item">1：平台管理费不退</view>
					<view class="item">2：视频可退剩余条数*单条金额</view>
					<view class="item">3：产品服务费可退剩余未销售产品服务费*单件</view>
					<view class="item" v-if="channelId!=='2'">4：商家宣传收益可退剩余条数*单条金额</view>
					
				</uni-collapse-item>
			</uni-collapse>
			
<!-- 			<view class="title-detail">费用明细</view>
			<view class="item" v-if="showProcutFee">
				<view class="title">产品服务费</view>
				<view class="title">{{serviceAmount?serviceAmount/100:0}}元</view>
			</view>
			<view class="item" v-if="showProcutFee&&userType==99">
				<view class="title">产品管理费</view>
				<view class="title">{{adsellAmount/100}}元</view>
			</view>
			<view class="item">
				<view class="title">平台收取佣金</view>
				<view class="title">{{platAmount/100}}元</view>
			</view>
			<view class="item">
				<view class="title">广告需支付金额</view>
				<view class="title">{{adAmount/100}}元</view>
			</view>
			<view class="item">
				<view class="title">代理商佣金金额</view>
				<view class="title">{{mchAmount/100}}元</view>
			</view>
			<view class="item">
				<view class="title">总计支付金额</view>
				<view class="title">{{totalAmount/100}}元</view>
			</view> -->


			<view class="pay-select">支付方式 </view>
			<view class="item" @click="selectPayType(true)">
				<view class="pay-type" :class="isWxpay?'pay-type-active':''">
					<image src="../../static/img/wx.png" mode=""></image>
					<view class="pay-name">微信</view>
				</view>
			</view>
			<view  class="item" @click="selectPayType(false)">
				<view class="pay-type" :class="!isWxpay?'pay-type-active':''">
					<image src="../../static/img/ali.png" mode=""></image>
					<view class="pay-name">支付宝</view>
				</view>
			</view>



			<view class="item">
				<button class="btn-confirm" type="default" hover-class="btn-confirm-active"
					@click="confirm">确定购买</button>
			</view>
		</view>


	</view>
</template>

<script>
	// import common from '../../common.js';
	export default {
		data() {
			return {
				amount: '0',
				isShowDetail: false,
				platAmount: '0',
				adAmount: '0',
				mchAmount: '0',
				totalAmount: '0',
				isWxpay: true,
				payType: '',
				number: '', //购买条数
				adId: '', //广告id
				totalFee:'总计费用：',
				totalPrice : 0,

				orderId: '',
				mainId: '', //用户id
				provider: '',
				showProcutFee:false  ,//是否显示
				serviceAmount:'', //产品服务费
				adsellAmount:'', //产品管理费
				userType:0, //用户身份
				isUsed:0, //是否使用优惠券
				channelId:0, //投放渠道
				disableInput:false

			}
		},
		onLoad(options) {
			this.adId = options.id
			this.amount = options.amount
			this.channelId = options.channelId
			if('sell'==options.type){
				console.log('type: sell...')
				this.showProcutFee = true
			}
			//是否使用小会员优惠券
			if('1'==options.isUsed){
				this.isUsed = 1
				this.number = 500
				this.disableInput = true
			}else{
				this.isUsed = 0
			}
			
			uni.getStorage({
				key: 'user',
				success: res=>{
					console.log('用户信息：',res)
					this.mainId = res.data.id;
					this.userType = res.data.type
					console.log("openid: ", this.mainId);
				}
			})
			// uni.getProvider({
			// 	service: 'payment',
			// 	success: (res) => {
			// 		this.provider = res.provider[0]
			// 		if ('wxpay' == this.provider) {
			// 			this.isWxpay = true
			// 		} else {
			// 			this.isWxpay = false
			// 		}
			// 	}
			// })
		},
		methods: {
			showDetail() {
				if (this.checkDatas()) {
					this.getBuyAdTradeInfo()
					//test
					// this.isShowDetail = true
				}

			},
			selectPayType(bool) {
				this.isWxpay = bool
			},
			checkDatas() {
				if (!this.number) {
					this.toast('请输入购买条数')
					return false
				}
				if (this.number < 100) {
					this.toast('购买条数最低为100条')
					return false
				}
				return true
			},
			getBuyAdTradeInfo() {
				let data = {
					adId: this.adId, //		广告ID		true							string
					payerId: this.mainId, //	用户ID（mainId）		true	string
					num: this.number, //	购买条数		true	integer
					plat: '6', //	支付平台		true	integer 平台类型
					payType: this.typen, //		支付方式		true	integer
				}
				console.log('请求参数：',data)
				this.$request.post(`mch/getBuyAdTradeInfo`, data,'json')
					.then(res => {
						console.log("请求计算费用 res:", res)
						if (res.data.status == 200) {
							this.isShowDetail = true
							this.totalAmount = res.data.data.totalAmount
							this.mchAmount = res.data.data.mchAmount
							this.platAmount = res.data.data.platAmount
							this.adAmount = res.data.data.adAmount
							this.serviceAmount = res.data.data.serviceAmount
							this.adsellAmount = res.data.data.adsellAmount
							let value = parseFloat(this.totalAmount)/100
							this.totalPrice = value;
							this.totalFee = `总计费用：${value}元`
						}
					}).catch(err => {
						console.log("请求计算费用 err:", err)
					})
				// common.request("mch/getBuyAdTradeInfo", data, res => {
				// 	console.log('请求计算费用：', res)
				// 	if (res.status == 200) {
				// 		this.isShowDetail = true
				// 		this.totalAmount = res.data.totalAmount
				// 		this.mchAmount = res.data.mchAmount
				// 		this.platAmount = res.data.platAmount
				// 		this.adAmount = res.data.adAmount
				// 	}
				// })

			},
			confirm() {
				
				let data = {
					adId: this.adId, //		广告ID		true							string
					payerId: this.mainId, //	用户ID（mainId）		true	string
					num: this.number, //	购买条数		true	integer
					plat: '1', //	支付平台		true	integer 平台类型
					payType: this.isWxpay ? '1' : '2', //		支付方式		true	integer
					isUsed:this.isUsed
				}
				console.log("请求订单参数: ",data);
				this.$request.post(`ad/buyVideoAd`, data,'json')
					.then(res => {
						console.log('获取订单信息：', res)
						this.isShowDetail = true
						this.orderId = res.data.data.trade.id
						if(this.isUsed && this.totalPrice == 0){
							this.callbackBuyAd()
							return
						}
						if(this.isWxpay){
							this.wxPay(res.data.data.third)
						}else{
							this.aliPay(res.data.data.third.body)
						}
					}).catch(err => {
						console.log(" err:", err)
					})
			},
			
			wxPay(orderInfo) {
				console.log("type: ",typeof(orderInfo));
				uni.requestPayment({
					provider: 'wxpay',
					orderInfo:orderInfo,
					// timeStamp: orderInfo.timestamp,
					// nonceStr: orderInfo.noncestr,
					// package: orderInfo.package,
					// signType: 'RSA',
					// paySign: orderInfo.sign,
					success: res => {
						console.log("支付成功: ", res);
						this.toast('支付成功')
						// 支付成功 确认购买广告订单
						this.callbackBuyAd();
					},
					fail: err => {
						console.log("支付失败: ", err);
						this.toast(err.errMsg)
					}
				})
			},

			aliPay(orderInfo) {
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: orderInfo,
					success: res => {
						this.toast('支付成功')
						this.callbackBuyAd();
					},
					fail: err => {
						console.log("支付失败: ", err);
						this.toast(err.errMsg)
					}
				})
			},
			//确认购买广告订单
			callbackBuyAd() {
				let data = {
					id: this.orderId,
					isUsed:this.isUsed
				}

				let url = 'ad/callbackBuyVideoAd/'
				if(this.channelId == "1"){
					url = 'ad/callbackBuyAd/'
				}

				this.$request.post(url + this.orderId +'/'+this.isUsed, data)
					.then(res => {
						console.log('确认购买：', res)
						if (res.data.status == 200) {
							this.toast('确认购买成功')
							//返回上一页面
							uni.switchTab({
								url: '/pages/index/index'
							})
						} else {
							this.toast(res.data.data.msg)
						}
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request(url + this.orderId, data, res => {
				// 		console.log('确认购买：', res)
				// 		if (res.status == 200) {
				// 			this.toast('确认购买成功')
				// 			//返回上一页面
				// 			uni.switchTab({
				// 				url: '/pages/main/index'
				// 			})
				// 		} else {
				// 			this.toast(res.data.msg)
				// 		}

				// 	},
				// 	err => {
				// 		console.log('确认购买err：', err)
				// 	})
			},

			backPage() {
				uni.navigateBack({
					delta: 3
				})
			},

			toast(msg) {
				uni.showToast({
					title: msg,
					icon: 'none',
					duration: 2000
				});
			},


		}
	}
</script>

<style>
	.publish-payment {
		background-color: #F1F1F1;
	}

	.publish-payment .item-wrap {
		background-color: white;
		width: 690upx;
		margin: 30upx;
		border-radius: 8upx;
		padding: 20upx 20upx;
	}

	.publish-payment .item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		/* margin-top: 30upx; */
		padding: 10upx 30upx;
	}

	.publish-payment .item .title {
		font-size: 28upx;
	}

	.publish-payment .item .input {
		width: 300upx;
		font-size: 28upx;
		background-color: #E8E8E8;
		height: 60upx;
		text-align: center;
		border: 6upx;
	}

	.publish-payment .item .button {
		width: 70%;
		height: 64upx;
		line-height: 64upx;
		border: 2upx solid #F1C75E;
		border-radius: 6upx;
		margin: 20upx auto;
		font-size: 30upx;
		color: #F1C75E;
		text-align: center;
		background-color: white;
	}

	.publish-payment .item .button-active {
		background-color: #F1C75E;
		color: white;
	}

	.publish-payment .title-detail {
		color: #F1C75E;
		text-align: center;
		font-weight: 600;
		font-size: 32upx;
	}

	.publish-payment .pay-select {
		font-size: 30upx;
		margin-top: 30upx;
	}

	.publish-payment .item .pay-type {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 90%;
		height: 72upx;
		color: #000000;
		border: 2upx solid #CCCCCC;
		border-radius: 8upx;
		font-size: 30upx;
		margin: 10upx auto;
	}

	.publish-payment .item .pay-type-active {
		color: #F1C75E;
		border: 2upx solid #F1C75E;
	}

	.publish-payment .item .pay-type image {
		width: 48upx;
		height: 48upx;
		margin: 0 20upx 0 30upx;
	}

	.publish-payment .item button::after {
		border: none;
	}

	.publish-payment .item .btn-confirm {
		width: 80%;
		height: 86upx;
		line-height: 86upx;
		background: -webkit-linear-gradient(#F1C75E, #d4a93c);
		background: -o-linear-gradient(#F1C75E, #d4a93c);
		background: -moz-linear-gradient(#F1C75E, #d4a93c);
		background: linear-gradient(#F1C75E, #d4a93c);
		margin: 0 auto;
		border-radius: 40upx;
		color: white;
		margin-top: 10upx;
		font-size: 30upx;
	}

	.publish-payment .item .btn-confirm-active {
		opacity: 0.5;
	}
	.pay-name{
		font-size: 28upx;
	}
</style>
